package com.clw.template.wx.service.impl;

import com.clw.template.wx.service.IWxAuthService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class WxAuthServiceImpl implements IWxAuthService {

    @Autowired
    private WxMpService wxMpService;


    @Override
    public String authorize(String returnUrl) {
//        wxMpService.oauth2buildAuthorizationUrl();
        return null;
    }

    @Override
    public String userInfo(String code, String returnUrl) throws Exception {
        /*log.info("【微信网页授权】code={}", code);
        log.info("【微信网页授权】state={}", returnUrl);
        WxMpOAuth2AccessToken wxMpOAuth2AccessToken;
        try {
            wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(code);
        } catch (WxErrorException e) {
            log.info("【微信网页授权】{}", e);
            throw new Exception(e.getError().getErrorMsg());
        }
        String openId = wxMpOAuth2AccessToken.getOpenId();
        log.info("【微信网页授权】openId={}", openId);
        return "redirect:" + returnUrl + "?openid=" + openId;*/
        return wxMpService.getAccessToken();
    }
}
